#include "stdafx.h"

Board::Board(Pieces *pPieces, int pScreenHeight){
	mPieces = pPieces;
	mScreenHeight = pScreenHeight;
	InitBoard();
}

int Board::GetXPosInPixels(int pPos){

return  ( ( BOARD_POSITION - (BLOCK_SIZE * (BOARD_WIDTH / 2)) ) + (pPos * BLOCK_SIZE) );  
	
}
int Board::GetYPosInPixels(int pPos){

	return ( (mScreenHeight - (BLOCK_SIZE * BOARD_HEIGHT)) + (pPos * BLOCK_SIZE) );  
}
bool Board::IsFreeBlock(int pX, int pY){
	if(mBoard[pX][pY]==0) return true; else return false;
}
bool Board::IsPossibleMovement(int pX, int pY, int pPiece, int pRotation){
	// Checks collision with pieces already stored in the board or the board limits
	// This is just to check the 5x5 blocks of a piece with the appropiate area in the board
	for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
	{
		for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
		{	
			// Check if the piece is outside the limits of the board
			if (	i1 < 0 			|| 
				i1 > BOARD_WIDTH  - 1	||
				j1 > BOARD_HEIGHT - 1)
			{
				if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0)
					return 0;		
			}

			// Check if the piece have collisioned with a block already stored in the map
			if (j1 >= 0)	
			{
				if ((mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0) &&
					(!IsFreeBlock (i1, j1))	)
					return false;
			}
		}
	}

	// No collision
	return true;
}
void Board::StorePiece(int pX, int pY, int pPiece, int pRotation){
		// Store each block of the piece into the board
	for (int i1 = pX, i2 = 0; i1 < pX + PIECE_BLOCKS; i1++, i2++)
	{
		for (int j1 = pY, j2 = 0; j1 < pY + PIECE_BLOCKS; j1++, j2++)
		{	
			// Store only the blocks of the piece that are not holes
			if (mPieces->GetBlockType (pPiece, pRotation, j2, i2) != 0)		
				mBoard[i1][j1] = POS_FILLED;	
		}
	}

}
void Board::DeletePossibleLines(){
	for(int j = BOARD_HEIGHT; j>0; j--){
		bool isLineFull = true;
		for(int i=0; i< BOARD_WIDTH; i++){
			
			if(mBoard[i][j] == POS_FREE) {
				isLineFull = false;
				break;
			}
		}


		if(isLineFull) DeleteLine(j);
	}

}
bool Board::IsGameOver(){

	for(int i = 0; i<BOARD_WIDTH; i++){
		if(mBoard[i][0] == POS_FILLED) return true;
	}

	return false;
}
void Board::InitBoard(){
	for(int i =0; i< BOARD_HEIGHT; i++){
		for(int j = 0; j< BOARD_WIDTH; j++){
			mBoard[j][i]=POS_FREE;	
		}
	}
}

void Board::DeleteLine (int pY){

	for(int j = pY; j>0; j--){
		for(int i=0; i< BOARD_WIDTH; i++){
			mBoard[i][j] = mBoard[i][j-1];
		}
	}

}